﻿//--------------------------------------------------
// Create WoW.Armory Javascript Object
//--------------------------------------------------
if ("undefined" == typeof (WoW)) {
    WoW = new Object();
}

if ("undefined" == typeof (WoW.Armory)) {
    WoW.Armory = new Object();
    WoW.Armory.Interop = new Gadget.Interop.Service();
    WoW.Armory.Armory = null;
}

WoW.Armory.Init = function() {
    try {
        //Check Gadget Settings
        if (this.Settings.IsFirstTime() == true) {
            System.Gadget.Settings.show = true;
        }
        else {
            //Add parameters on WoW.Armory.WoW.Armory constructor
            this.Interop.Service.AddConstructorParam(System.Gadget.Settings.readString("Region"));
            this.Interop.Service.AddConstructorParam(System.Gadget.Settings.readString("Realm"));
            this.Interop.Service.AddConstructorParam(System.Gadget.Settings.readString("CharName"));

            //Load .NET Assembly(WoW.Armory.Armory) with parameters
            this.Armory = this.Interop.Service.LoadTypeWithParams(System.Gadget.path + "\\bin\\WoW.Armory.dll", "WoW.Armory.Armory", false);

            var frame = document.getElementById("Battlegroup");
            frame.innerHTML = this.Armory.Character.Battlegroup;
            frame = document.getElementById("Realm");
            frame.innerHTML = this.Armory.Character.Realm;
            frame = document.getElementById("Prefix");
            frame.innerHTML = this.Armory.Character.Prefix;
            frame = document.getElementById("CharName");
            frame.innerHTML = this.Armory.Character.Name;
            frame = document.getElementById("GuildName");
            frame.innerHTML = this.Armory.Character.GuildName;
            frame = document.getElementById("ClassName");
            frame.innerHTML = "Level " + this.Armory.Character.Level + " " + this.Armory.Character.Race + " " + this.Armory.Character.ClassName;
            frame = document.getElementById("ClassImage");
            frame.src = this.Armory.Character.ImageURL;
        }
    }
    catch (e) {
        var frame = document.getElementById("gFrame");
        frame.innerText = e.message;
    }
}


//--------------------------------------------------
// Create WoW.Armory.Settings Javascript Object
//--------------------------------------------------
if ("undefined" == typeof (WoW.Armory.Settings)) {
    WoW.Armory.Settings = new Object();
}

// --------------------------------------------------------------------
// Verify if is fisrt time
// --------------------------------------------------------------------
WoW.Armory.Settings.IsFirstTime = function() {
    if (System.Gadget.Settings.readString("WoWArmoryFirstTime") == "")
        return true;
    else
        return false;
}

// --------------------------------------------------------------------
// Set Default Values
// --------------------------------------------------------------------
WoW.Armory.Settings.SetDefaultValues = function() {
    System.Gadget.Settings.write("Region", "US");
    System.Gadget.Settings.write("Realm", "Nazjatar");
    System.Gadget.Settings.write("CharName", "Thirith");
    System.Gadget.Settings.write("Refresh", "5");
    System.Gadget.Settings.write("WoWArmoryFirstTime", false);
}

// --------------------------------------------------------------------
// Handle the Settings dialog show event.
// --------------------------------------------------------------------
WoW.Armory.Settings.OnShow = function() {
    if (WoW.Armory.Settings.IsFirstTime() == true)
        WoW.Armory.Settings.SetDefaultValues();
}

// --------------------------------------------------------------------
// Handle the Settings dialog closed event.
// event = System.Gadget.Settings.ClosingEvent argument.
// --------------------------------------------------------------------
WoW.Armory.Settings.OnClosed = function(event) {
    if (event.closeAction == event.Action.commit) {
        WoW.Armory.Init();
    }
    else if (event.closeAction == event.Action.cancel) {
        //Do nothing
    }
}

//-----------------------------------------------
// Enable the gadget settings functionality.
//-----------------------------------------------
System.Gadget.settingsUI = "Settings.html";
//-----------------------------------------------
// Delegate for the show settings event.
//-----------------------------------------------
System.Gadget.onShowSettings = WoW.Armory.Settings.OnShow;
//-----------------------------------------------
// Delegate for the settings closed event.
//-----------------------------------------------
System.Gadget.onSettingsClosed = WoW.Armory.Settings.OnClosed;